<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">机构管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a><cite>系统管理</cite></a>
          <a><cite>机构管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <!-- 搜索条件 -->
        <div class="layui-form toolbar">
            所属公司：<select id="company" lay-search lay-filter="company">></select>&nbsp;&nbsp;&nbsp;
            <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="operate"><i
                    class="layui-icon">&#xe63c;</i>操作日志
            </button>
        </div>
        <div id="orgChart"></div>
    </div>
</div>
<link href="/assets/libs/orgChart/org.css" media="all" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/assets/libs/orgChart/org.js"></script>
<script type="text/javascript">
    // 加载机构树
    function loadOrg(crab, companyId) {
        crab.get('/sys/org/list_' + companyId, {}, function (data) {
            $('#orgChart').orgChart({
                data: data,
                showControls: true,
                newNodeText: '添加子部门',
                onAddNode: function (node) {
                    editOrg(false, node);
                    //orgChart.newNode(node.data.id);
                },
                onDeleteNode: function (node) {
                    layer.confirm('确定要删除该机构信息吗？', function () {
                        crab.delete('/sys/org/' + node.data.id, {}, function (data) {
                            layer.msg('删除成功', {icon: 1}, function () {
                                // 重新渲染
                                loadOrg(crab, companyId);
                            });
                        });
                    });
                },
                onClickNode: function (node) {
                    editOrg(true, node);
                }
            });
        });

        function editOrg(edit, node) {
            var data = edit ? node.data : {pid: node.data.id};
            data.edit = edit;
            data.companyId = $('#company').val();
            crab.putFormData(data);
            crab.popupCenter({
                title: edit ? '编辑机构' : '添加机构',
                path: '/components/sys/org-form.html',
                finish: function () {
                    // 重新渲染
                    loadOrg(crab, data.companyId);
                }
            });
        }
    }

    layui.use(['form', 'table', 'config', 'crab'], function () {
        var form = layui.form;
        var crab = layui.crab;

        // 监听选择企业
        form.on('select(company)', function (data) {
            loadOrg(crab, data.value);
        });

        // 默认加载
        crab.get('/sys/company/list_effective', {}, function (data) {
            var l = data.length;
            if (l > 0) {
                var option = '';
                for (var i = 0; i < l; i++) {
                    option += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                }
                $('#company').append(option);
                form.render('select');
                // 加载机构
                loadOrg(crab, data[0].id);
            }
        });
    });
</script>
